home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / gateway.idb / usr / WebFace / Source / 20-NetworkServices / dhcp / dhcp-relay.frm.z / dhcp-relay.frm
Encoding:
Text File  |  1997-07-30  |  10.4 KB  |  385 lines

  1. #!/usr/bin/perl5
  2. #
  3. # dhcp-relay.cgi
  4. #
  5. # Copyright 1988-1996 Silicon Graphics, Inc.
  6. # All rights reserved.
  7. #
  8. # This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  9. # the contents of this file may not be disclosed to third parties, copied or
  10. # duplicated in any form, in whole or in part, without the prior written
  11. # permission of Silicon Graphics, Inc.
  12. #
  13. # RESTRICTED RIGHTS LEGEND:
  14. # Use, duplication or disclosure by the Government is subject to restrictions
  15. # as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  16. # and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  17. # successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  18. # rights reserved under the Copyright Laws of the United States.
  19. #
  20. # $Id: dhcp-relay.frm,v 1.31 1997/06/19 22:25:05 shotes Exp $
  21.  
  22. # To determine that dhcp_relay is currently enabled, the uncommented line
  23. # must exist in inetd.conf and chkconfig proclaim_relayagent must be on.
  24. #
  25. # To enable, both the above are set, and a sig HUP is sent.
  26. #
  27. # To disable, the line is commented and chkconfig p... is set off and sig 
  28. # HUP sent. 
  29.  
  30. BEGIN { require "/usr/WebFace/lib/CGI.pm"; import CGI; }
  31. require "/usr/OnRamp/lib/OnRamp.pm";
  32. require "/usr/OnRamp/lib/java.pm";
  33.  
  34. $title = "DHCP Relay Agent";
  35.  
  36. $js = 
  37. "type = \"server\";
  38. $js_account_main
  39. $js_error_box
  40. $js_ip
  41. $js_hostname
  42. function testAdd(form)  {
  43.     Ctrl = form.add_list;
  44.     if (Ctrl.value == \"\") {
  45.         errorBox (Ctrl, \"To add a new server, first enter the \"
  46.             + \"hostname \\nor IP address, then click \"
  47.             + \"the \\\"Add New Server\\\" \\nbutton.\");
  48.         return (false);
  49.     }
  50.     if (!testThisHost(form.add_list)) return (false);
  51.     return (true);
  52. }
  53. function testThisHost(Ctrl) {
  54.     start = 0;  
  55.     while (Ctrl.value.charAt(start) == ' ') { start++; }
  56.     c = Ctrl.value.charAt(start);
  57.     if (! checkInt_c(c, 10)) { // this is not an IP address
  58.         if (!testHostname(Ctrl, Ctrl.value, \"hostname\", 0)) return (false);
  59.         return (true);
  60.     } else {
  61.         if (!testIPaddress(Ctrl.value, false)) {
  62.             errorBox(Ctrl, \"Invalid IP address.\");
  63.             return (false);
  64.         }
  65.     } 
  66.     return (true);
  67. }";
  68.  
  69.  
  70. $query = new CGI;
  71. print $query->header;
  72.  
  73. $help = $document_root . $ENV{"SCRIPT_NAME"};
  74. $help =~ s/cgi$/hlp/;
  75. exec $help if ($query->param('help') eq "Help");
  76.  
  77. &js_title_block($title,$js);
  78.  
  79. $def_config_dir = "/var/dhcp/config";
  80. $rly_off = system ("/etc/chkconfig", "proclaim_relayagent");
  81. $srv_off = system ("/etc/chkconfig", "proclaim_server");
  82. $def_rstate = "No";
  83. if ($rly_off == 0) {
  84.     $def_rstate = "Yes";
  85. }
  86. $inetd_cf = "/etc/inetd.conf";
  87. $dummy = "/etc/inetd.conf.tmp";
  88.  
  89. # Only look at the option file name in /etc/inetd.conf if the server is
  90. # enabled, also need to process it then.
  91. if ($rly_off == 0) {    # chkconfig proclaim_relayagent is on
  92.     open(INETD, $inetd_cf) || &error("Cannot open $inetd_cf");
  93.     while (<INETD>) {
  94.     if (/^#/) {
  95.         next;
  96.     }
  97.     # bootp dgram udp wait root /usr/etc/dhcp_relay dhcp_relay -o <file>
  98.     if (/^bootp/) {
  99.         chop;
  100.         @entlist = split(/\s+/);
  101.         if (($entlist[6] eq "dhcp_relay") && ($entlist[7] eq "-o")) {
  102.         $osf = $entlist[8];
  103.         last;    # Break out of the while loop
  104.         }
  105.     }
  106.     }
  107.     close(INETD);
  108. }
  109.  
  110. if (($rly_off == 0) && $osf) {
  111.     open(OPTIONS, $osf) || &error("Cannot open $osf");
  112.     while(<OPTIONS>) {
  113.     if (/^#/) {
  114.         next;
  115.     }
  116.     chop;
  117.     @optlist = split(/\s+/);
  118.     $i = 0;
  119.     while ($optlist[$i]) {
  120.         if ($optlist[$i] eq "-c") {
  121.         $def_config_dir = $optlist[$i+1];
  122.         $i += 2;
  123.         next;
  124.         }
  125.         else {
  126.         print $err_start,
  127.               "Unknown Option $optlist[$i] in the <b>$osf</b> file.";
  128.         }
  129.         $i++;
  130.     }
  131.     }
  132.     close(OPTIONS);
  133. }
  134. else {
  135.     $osf = "/etc/config/dhcp_relay.options";
  136. }
  137.  
  138. $srvlist_file = $def_config_dir."\/dhcp_relay.servers";
  139.  
  140. open(SRVLST, $srvlist_file) || &error("Cannot open $srvlist_file");
  141. $i = 0;
  142. while (<SRVLST>) {
  143.     if (/^#/) {
  144.     next;
  145.     }
  146.     chop;
  147.     $_ =~ s/^\s+//;
  148.     $_ =~ s/\s+$//;
  149.     $server_list[$i] = $_;
  150.     $i++;
  151. }
  152. close(SRVLST);
  153.  
  154. undef @new_list;
  155.  
  156. $addServer = 0;
  157. $delServer = 0;
  158.  
  159. if ($query->param) {
  160.     if ($query->param('add') eq 'Add New Server') {
  161.         $addList = $query->param('add_list');
  162.         &formValid_add;
  163.         $message = qq|Click "Ok" to save changes.|;
  164.         $query->param('add_list','');
  165.         &generic($addList,1);
  166.         exit 0;
  167.     }
  168.  
  169.     elsif ($query->param('delete') eq 'Delete Existing Server') {
  170.         $delList = $query->param('chosen');
  171.         &error("To delete an existing server, first select one from
  172.         the list, then click the delete button.") if !$delList;
  173.         $delList =~ /([\w.-]+)/;  
  174.         $delList = $1;
  175.         $message = qq|Click "Ok" to save changes.|;
  176.         &generic($delList,2);
  177.         exit 0;
  178.     }
  179.  
  180.     elsif ($query->param('doit') eq 'Ok') {
  181.         if ($query->param('addName')) { 
  182.             push(@server_list,$query->param('addName'));
  183.             &saveList;
  184.             $message = "Server added."; 
  185.         }
  186.         if ($query->param('delName')) {
  187.             $delName = $query->param('delName');
  188.             undef @temp_list;
  189.             foreach $arg (@server_list) {
  190.                 if ($arg ne $delName) { push(@temp_list,$arg); }
  191.             }
  192.             @server_list = @temp_list;
  193.             &saveList; 
  194.             $message = "Server deleted."; 
  195.         }
  196.  
  197.         $new_rstate = $query->param(rstate);
  198.  
  199.         if ($new_rstate ne $def_rstate) {    # State Changed
  200.             # Need to make sure that DHCP Server is disabled
  201.             if ($new_rstate eq "Yes") {
  202.                 # chkconfig on, the system WILL ship with the inetd.conf entry
  203.                 # for dhcp_bootp enabled
  204.                 if ($srv_off == 0) {
  205.                     &error("You Need to Disable the DHCP Server before you
  206.                     can enable the DHCP Relay Agent.");
  207.                 }
  208.                 system ("/etc/chkconfig", "autoconfig_ipaddress", "off");
  209.                 system ("/etc/chkconfig", "proclaim_relayagent", "on");
  210.                 &checkForInetd;
  211.                 $message .= "  DHCP relay agent enabled.";
  212.             }
  213.             else {
  214.                 # chkconfig off
  215.                 system ("/etc/chkconfig", "proclaim_relayagent", "off");
  216.                 &commentInetd;
  217.                 $message .= "  DHCP relay agent disabled.";
  218.             }
  219.             system("/etc/killall", "-HUP", "inetd");
  220.         }
  221.         if (!$message) { $message = "No changes made."; }
  222.     }
  223.     else {
  224.         $message = "Use buttons to submit form.";
  225.         &generic;
  226.         exit 0;
  227.     }
  228. }
  229.  
  230. sub commentInetd {
  231.     open(IN,"< $inetd_cf");
  232.     open(OUT,"> $dummy");
  233.     while(<IN>) {
  234.     @items = split(/\s+/);
  235.     if ($items[0] eq "bootp" && $items[6] eq "dhcp_relay") {
  236.         print OUT "# $_";
  237.     } else { print OUT $_; }
  238.     }
  239.     close(IN);
  240.     close(OUT);
  241.     rename($dummy,$inetd_cf);
  242. }    
  243.  
  244. sub checkForInetd {
  245.     $line = "bootp\tdgram\tudp\twait\troot\t/usr/etc/dhcp_relay ".
  246.         "dhcp_relay -o /etc/config/dhcp_relay.options";
  247.     $rename = 0; $found = 0;
  248.     open(IN,"< $inetd_cf");
  249.     open(OUT,"> $dummy");
  250.     while(<IN>) {
  251.     @items = split(/\s+/);
  252.     if ($items[6] eq "dhcp_relay" || $items[7] eq "dhcp_relay") {
  253.         $found = 1; 
  254.         if (substr($items[0],0,1) eq "#") {
  255.         print OUT "$line\n";
  256.         $rename = 1;
  257.         }
  258.     } else { print OUT $_; }
  259.     }
  260.     if (!$found) { print OUT "$line\n"; $rename = 1; }
  261.     close(IN);
  262.     close(OUT);
  263.     if ($rename) { rename($dummy,$inetd_cf); }
  264. }
  265.  
  266. sub formValid_add {
  267.     &error("Server name or address required.") if !$addList;
  268.     if ($addList =~ /[^0-9.]/) {    # assume this is not an IP number 
  269.     &error("Please input IP address in dot notation.") if $addList =~ /0x/;
  270.     &error("Invalid hostname: $addList.") if &check_hostname($addList);
  271.     ($name, $aliases, $addrtype, $length, @addrs) = gethostbyname($addList);
  272.     &error("Host cannot be resolved.") if !@addrs;
  273.     } else {                # IP num
  274.     &error("Invalid IP address.") if &check_ipaddr($addList);    
  275.     }
  276. }
  277.  
  278. sub saveList {
  279.     open(SRVLST, ">$srvlist_file") ||
  280.     &error("Cannot open file $srvlist_file for writing.");
  281.     foreach $arg (@server_list) {
  282.     print SRVLST "$arg\n";
  283.     }
  284.     close(SRVLST);
  285. }    
  286.  
  287. &generic;
  288.  
  289. sub generic {
  290.     &header_block($title);
  291.  
  292.     print "<i>$message</i>";
  293.  
  294.     print "<form method=post name=AccountForm onSubmit=\"return runSubmit()\">\n";
  295.  
  296.     print "<center><table cellpadding=5 width=450>\n";
  297.  
  298.     print "<tr><th align=left>Enable DHCP relay agent:</th><th align=left>\n",
  299.       $query->radio_group(-name=>'rstate',
  300.               -values=>['Yes','No'],
  301.               -default=>$def_rstate),
  302.       "</th></tr>";
  303.  
  304.     print '<tr><th align=left><input type=submit name="add" ',
  305.           'value="Add New Server" onClick="return markAdd()"></th><td>',
  306.           $query->textfield(-name=>'add_list', -size=>20),'</td></tr>';
  307.  
  308.     if ($_[1] == 1) {
  309.     print "<input type=hidden name=addName value=$_[0]>"; 
  310.     @locList = @server_list;
  311.     push(@locList,$_[0]);
  312.     } elsif ($_[1] == 2) {
  313.     print "<input type=hidden name=delName value=$_[0]>"; 
  314.     undef @locList;
  315.     foreach $arg (@server_list) {
  316.         if ($arg ne $_[0]) { push(@locList,$arg); }
  317.     }
  318.     } else { @locList = @server_list; }
  319.  
  320.     print '<tr><th align=left valign=top><input type=submit name="delete" ',
  321.           'value="Delete Existing Server" onClick="return markDelete()">',
  322.           '</th><td>',&choice_list(*locList,"chosen",20),'</td></tr>';
  323.     
  324.     print "</table><br>";
  325.  
  326.     print &js_buttons('doit','Ok','onClick="markOther()"','onClick="markOther()"');
  327.  
  328.     print $query->endform;
  329. }
  330.  
  331. sub oldgeneric {
  332.     &header_block($title);
  333.  
  334.     print "<i>$message</i>";
  335.  
  336.     print $query->startform;
  337.  
  338.     print "<center><table cellpadding=5 width=450>\n";
  339.  
  340.     print "<tr><th align=left>Enable DHCP relay agent:</th><th align=left>\n",
  341.       $query->radio_group(-name=>'rstate',
  342.               -values=>['Yes','No'],
  343.               -default=>$def_rstate),
  344.       "</th></tr>";
  345.  
  346.     print "<tr><th align=left>Directory for server configuration files:</th>",
  347.         "<th align=left>\n",
  348.       $query->textfield(-name=>'conf_dir',
  349.                 -default=>$def_config_dir,
  350.                 -size=>20, -maxlength=>256),
  351.       "</th></tr>";
  352.  
  353.     print "<tr><th align=left>List of DHCP Servers:</th><th align=left>\n",
  354.       $query->scrolling_list(-name=>'slist', -values=>\@server_list,
  355.                      -size=>6, -multiple=>'true'),
  356.       "</th></tr>";
  357.  
  358.     print "<tr><th align=left>Delete Selected Servers from the List:</th><th align=left>\n",
  359.       $query->radio_group(-name=>'delstate',
  360.                   -values=>['Yes','No'],
  361.                   -default=>'No'),
  362.       "</th></tr>";
  363.  
  364.     print "<tr><th align=left>List of new DHCP Servers to Add:</th>",
  365.       "<th align=top>",
  366.       $query->textfield(-name=>'add_list',
  367.                 -size=>20, -maxlength=>256),
  368.       "</th></tr>";
  369.  
  370.     print "</table></center><br>";
  371.  
  372.     &button_table($query,"doit", "Ok", "help", "Help");
  373.     print $query->endform;
  374.  
  375. }
  376.  
  377.  
  378. sub
  379. error
  380. {
  381.     &error_block($_[0]);
  382.     &generic;
  383.     exit 0;
  384. }
  385.